/*
一元多项式的乘法与加法运算 
*/

#include <stdio.h>
#include <stdlib.h>
typedef struct Node *pNode;
struct Node {
    int coef;
    int expon;
    pNode next;
};

void attach(int c, int e, pNode *prear) {
    pNode p  = (pNode)malloc(sizeof(struct Node));
    p->coef  = c;
    p->expon = e;
    p->next = NULL;
    (*prear)->next = p;
    (*prear) = p;
}

pNode read_poly() {
    pNode p, rear, temp;
    int num;
    scanf("%d", &num);
    p = rear = (pNode)malloc(sizeof(struct Node));
    int c, e;
    for (int i = 0; i < num; i++){
        scanf("%d %d", &c, &e);
        attach(c, e, &rear);
    }
    temp = p;
    p = p->next;
    free(temp);
    return p;
}

pNode add(pNode p1, pNode p2) {
    pNode p, rear, temp;
    p = rear = (pNode)malloc(sizeof(struct Node));
    while (p1 && p2) {
        if (p1->expon == p2->expon) {
            int coef_sum = p1->coef + p2->coef;
            if (coef_sum != 0) {
                attach(coef_sum, p1->expon, &rear);
            }
            p1 = p1->next;
            p2 = p2->next;
        } else if ((p1->expon) > (p2->expon)) {
            attach(p1->coef, p1->expon, &rear);
            p1 = p1->next;
        } else {
            attach(p2->coef, p2->expon, &rear);
            p2 = p2->next;
        }
    }
    for (;p1; p1 = p1->next) {attach(p1->coef, p1->expon, &rear);}
    for (;p2; p2 = p2->next) {attach(p2->coef, p2->expon, &rear);}
    temp = p;
    p = p->next;
    free(temp);
    return p;
}

void free_node(pNode *p) {
    pNode temp;
    while (*p) {
        temp = (*p)->next;
        free(*p);
        *p = temp;
    }
}
pNode product(pNode p1, pNode p2) {
    pNode p_init, rear_init, temp_init;
    pNode p2_head = p2;
    int flag = 0;
    for (; p1; p1=p1->next){
        int coef1 = p1->coef;
        int expon1 = p1->expon;
        pNode per_p, per_rear, per_temp;
        per_p = per_rear = (pNode)malloc(sizeof(struct Node));
        p2 = p2_head;
        for (; p2; p2=p2->next) {
            int coef2 = p2->coef;
            int expon2 = p2->expon;
            if (coef1 * coef2){
                attach(coef1 * coef2, expon1 + expon2, &per_rear);
            }
        }
        per_temp = per_p;
        per_p = per_p->next;
        free(per_temp);
        if (!flag) {
            p_init = per_p;
            flag = 1;
        }
        else {
            temp_init = add(p_init, per_p);
            free_node(&per_p);
            free_node(&p_init);
            p_init = temp_init;
        }
    }
    return p_init;
}

void print_poly(pNode p) {
    int flag = 0;
    if (!p) {
        printf("0 0\n");
        return;
    }
    while (p) {
        if(!flag) 
            flag = 1;
        else 
            printf(" ");
        printf("%d %d", p->coef, p->expon);
        p = p->next;
    }
    printf("\n");
}

int main(int argc, char *argv[]) {
    pNode poly1 = read_poly();
    pNode poly2 = read_poly();

    pNode p_plus = product(poly1, poly2);
    print_poly(p_plus);
    pNode p_sum = add(poly1, poly2);
    print_poly(p_sum);
}